package com.hcinfos.gsoo.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.adapter.HistoryAdapter;
import com.hcinfos.gsoo.db.NewDBUtil;
import com.hcinfos.gsoo.enity.HistoryKeyword;
import com.hcinfos.gsoo.util.IntentUtil;

public class SearchActivity extends Activity implements OnClickListener {

	private ImageView back_iv;
	private EditText search_et;
	private Button search_bt;
	private RelativeLayout clear_rl;
	private NewDBUtil dbUtil;
	private ListView listView;
	private List<HistoryKeyword> list;
	private HistoryAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);

		dbUtil = NewDBUtil.newInstance(this);
		back_iv = (ImageView) findViewById(R.id.back_iv);
		back_iv.setOnClickListener(this);
		search_et = (EditText) findViewById(R.id.search_et);
		search_bt = (Button) findViewById(R.id.search_bt);
		search_bt.setOnClickListener(this);
		clear_rl = (RelativeLayout) findViewById(R.id.clear_rl);
		clear_rl.setOnClickListener(this);
		listView = (ListView) findViewById(R.id.listView);

		list = new ArrayList<HistoryKeyword>();

		adapter = new HistoryAdapter(this, list);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				IntentUtil.start_activity(SearchActivity.this,
						SearchResultActivity.class, new BasicNameValuePair(
								"keyword", list.get(arg2).getKeyword()));
			}
		});
	}

	@Override
	protected void onResume() {
		list.clear();
		refreshData();
		super.onResume();
	}

	private void refreshData() {
		try {
			List<HistoryKeyword> searchList = dbUtil.db
					.findAll(HistoryKeyword.class);
			if (searchList != null) {
				list.addAll(searchList);
			}
			if (list.size() == 0) {
				clear_rl.setVisibility(View.GONE);
			} else {
				clear_rl.setVisibility(View.VISIBLE);
			}
			adapter.setDataChanage(list);
		} catch (Exception e) {
		}
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.clear_rl:
			try {
				dbUtil.db.deleteAll(HistoryKeyword.class);
				list.clear();
			} catch (Exception e) {
			}
			refreshData();
			break;
		case R.id.search_bt:
			String keyword = search_et.getText().toString();
			if (keyword == null || keyword.length() == 0) {
				Toast.makeText(this, "请输入你要搜索的关键字", 500).show();
				return;
			}
			saveData(keyword);
			IntentUtil.start_activity(SearchActivity.this,
					SearchResultActivity.class, new BasicNameValuePair(
							"keyword", keyword));
			break;
		case R.id.back_iv:
			finish();
			break;
		}
	}

	private void saveData(String keyword) {
		HistoryKeyword history = new HistoryKeyword();
		history.setKeyword(keyword);
		try {
			dbUtil.db.saveOrUpdate(history);
		} catch (Exception e) {

		}
	}

}
